package cn.iocoder.yudao.module.blog.controller.app.jiucase;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.blog.controller.admin.jiucase.vo.BlogJiuCaseCreateReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.jiucase.vo.BlogJiuCasePageReqVO;
import cn.iocoder.yudao.module.blog.controller.admin.jiucase.vo.BlogJiuCaseUpdateReqVO;
import cn.iocoder.yudao.module.blog.controller.app.jiucase.vo.AppBlogJiuCaseAuditCreateVO;
import cn.iocoder.yudao.module.blog.controller.app.jiucase.vo.AppBlogJiuCaseRespVO;
import cn.iocoder.yudao.module.blog.convert.jiucase.BlogJiuCaseConvert;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiucase.BlogJiuCaseDO;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutag.BlogJiuTagDO;

import cn.iocoder.yudao.module.blog.service.jiucase.BlogJiuCaseService;
import cn.iocoder.yudao.module.blog.service.jiucaselevel.BlogJiuCaseLevelService;
import cn.iocoder.yudao.module.blog.service.jiutag.BlogJiuTagService;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "用户 APP - 案例")
@RestController
@RequestMapping("/blog/case")
@Validated
@Slf4j
public class AppBlogJiuCaseController {

    @Resource
    private BlogJiuCaseService blogJiuCaseService;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private BlogJiuTagService blogJiuTagService;

    @Resource
    private BlogJiuCaseLevelService blogJiuCaseLevelService;

    @PostMapping("/create")
    @Operation(summary = "创建案例")
    public CommonResult<Long> createPost(@Valid @RequestBody BlogJiuCaseCreateReqVO createReqVO) {
        return success(blogJiuCaseService.createJiuCase(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "修改案例")
    public CommonResult<Long> updatePost(@Valid @RequestBody BlogJiuCaseUpdateReqVO updateReqVO) {
        blogJiuCaseService.updateJiuCase(updateReqVO);
        return success(updateReqVO.getId());
    }

    @GetMapping("/page")
    @Operation(summary = "获得案例分页")
    public CommonResult<PageResult<AppBlogJiuCaseRespVO>> getPostPage(@Valid BlogJiuCasePageReqVO pageVO) {
        PageResult<BlogJiuCaseDO> pageResult = blogJiuCaseService.getJiuCasePage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogJiuCaseDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        Set<Long> tagIds = new HashSet<>();
        for(int i = 0; i<pageResult.getList().size(); i++){

            if(pageResult.getList().get(i).getTags() != null && StringUtil.isNotEmpty(pageResult.getList().get(i).getTags())){
                String[] ids = pageResult.getList().get(i).getTags().split(";");
                for(int j = 0; j<ids.length; j++){
                    tagIds.add(Long.parseLong(ids[j]));
                }
            }
        }
        List<BlogJiuTagDO> tagList = blogJiuTagService.getJiuTagList(tagIds);
        Map<Long, BlogJiuTagDO> tagMap = convertMap(tagList, BlogJiuTagDO::getId);

        return success(BlogJiuCaseConvert.INSTANCE.convertAppPage(pageResult, memberMap, tagMap));
    }

    @GetMapping("/get")
    @PermitAll
    @Operation(summary = "获得灸法案例")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<AppBlogJiuCaseRespVO> getJiuCase(@RequestParam("id") Long id) {
        BlogJiuCaseDO jiuCase = blogJiuCaseService.getJiuCase(id);
        MemberUserRespDTO memberUserRespDTO = memberUserApi.getUser(jiuCase.getMemberId());
        blogJiuCaseService.updateCaseReadNum(id, 1);

        Set<Long> tagIds = new HashSet<>();
        if(jiuCase.getTags() != null && StringUtil.isNotEmpty(jiuCase.getTags())){
            String[] ids = jiuCase.getTags().split(";");
            for(int i = 0; i<ids.length; i++){
                tagIds.add(Long.parseLong(ids[i]));
            }
        }

        List<BlogJiuTagDO> tagList = blogJiuTagService.getJiuTagList(tagIds);
        Map<Long, BlogJiuTagDO> tagMap = convertMap(tagList, BlogJiuTagDO::getId);

        return success(BlogJiuCaseConvert.INSTANCE.convertApp(jiuCase, memberUserRespDTO, tagMap));
    }

    @PostMapping("/audit")
    @Operation(summary="审核案例等级")
    public CommonResult<Boolean> auditCaseLevel(@Valid @RequestBody AppBlogJiuCaseAuditCreateVO reqVO){
        blogJiuCaseService.auditCase(reqVO.getCaseId(), reqVO.getLevelId());
        return success(true);
    }

    @GetMapping("/get-audiopage")
    @Operation(summary="获取音频列表")
    public CommonResult<PageResult<AppBlogJiuCaseRespVO>>  getJiuCaseAudioPage(@Valid BlogJiuCasePageReqVO pageVO){
        PageResult<BlogJiuCaseDO> pageResult = blogJiuCaseService.getJiuCaseAudioPage(pageVO);

        // 查询用户信息
        Set<Long> memberIds = convertSet(pageResult.getList(), BlogJiuCaseDO::getMemberId);
        Map<Long, MemberUserRespDTO> memberMap = memberUserApi.getUserMap(memberIds);

        Set<Long> tagIds = new HashSet<>();
        for(int i = 0; i<pageResult.getList().size(); i++){

            if(pageResult.getList().get(i).getTags() != null && StringUtil.isNotEmpty(pageResult.getList().get(i).getTags())){
                String[] ids = pageResult.getList().get(i).getTags().split(";");
                for(int j = 0; j<ids.length; j++){
                    tagIds.add(Long.parseLong(ids[j]));
                }
            }
        }
        List<BlogJiuTagDO> tagList = blogJiuTagService.getJiuTagList(tagIds);
        Map<Long, BlogJiuTagDO> tagMap = convertMap(tagList, BlogJiuTagDO::getId);

        return success(BlogJiuCaseConvert.INSTANCE.convertAppPage(pageResult, memberMap, tagMap));
    }

}
